home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 00 - Game Programming Primer / HelloWorld4.c < prev    next >
Text File  |  1995-06-29  |  24KB  |  515 lines

  1. //==============================================================================================\\
  2. //        -----------------------------------------------------------------------------------        \\
  3. //        HelloWorld4.c version 1.0.0    copyright © 1993…1995 Jamie McCornack, john calhoun            \\
  4. //        -----------------------------------------------------------------------------------        \\
  5. //         Demo program for Macintosh GameWriter 1.0.0, a training program…                        \\
  6. //        …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,…        \\
  7. //        …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment…    \\
  8. //        …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc.    \\
  9. //         A tutorial is available in Tricks of the Mac Game Programming Gurus, published…        \\
  10. //        …by Hayden Books, August 1995.                                                            \\
  11. //                                                                                                \\
  12. //        This code is offered by the copyright holders for no fee and for whatever use…            \\
  13. //        …you care to make of it, but we do hope you remember where it came from.                \\
  14. //                                                                                                \\
  15. //        Please send bug reports to MacGameDev at America OnLine.    macgamedev@aol.com            \\
  16. //        Suggestions and observations are also appreciated.                                        \\
  17. //        Updates and upgrades will be available now and then from the above e-mail address.        \\
  18. //==============================================================================================\\
  19.  
  20. // This program opens a window, displays an 8-bit color background, and at first…
  21. // …mouseclick, runs two clams across the screen. At second mouseclick, one clam stops,…
  22. // …talks, and flaps its/his/her face for 40 frames as the other clam continues running.
  23. // When the other clam runs up to the standing clam, there is an impact.
  24.  
  25.  
  26. #include "MGWExterns1.h"
  27.  
  28. #define     kPutInFront    (WindowPtr)-1L
  29. #define     kWaitTicks     4L        // Sets the delay in Ticks between frames. Try 3. Try 2. Try 0.
  30. #define     kJaneStepLength    12    // Sets the distance in pixels between Clamity Jane's moves.
  31. #define     kClemStepLength    16    // Sets the distance in pixels between Clem the Clam's moves.
  32. #define  kFrontFace            0    // Enumerates the various faces of the clam sprites.
  33. #define  kBlinkFace         1
  34. #define  kEehFace            2
  35. #define  kOohFace            3
  36. #define  kStepRightFace        4
  37. #define  kWalkRightFace        5
  38. #define  kRunRightFace        6
  39. #define  kDizzyRightFace1    7
  40. #define  kDizzyLeftFace1    8
  41. #define  kDizzyRightFace2    9
  42. #define  kDizzyLeftFace2    10
  43. #define  kMaxFaces            11
  44. #define  kBounce            -2    // Sets how many steps the running clam bounces back on impact.
  45. #define  kLastFace        40        // Sets how many frames the clams stay visible after impact.
  46.         
  47.                 // The resource constants--with 'r' prefix like Apple wants them.
  48. #define  rJaneFacesID    136        // The 'PICT' ID# where the views of Jane are located.
  49. #define  rClemFacesID    135        // The 'PICT' ID# where the views of Clem are located.
  50. #define  rMasksID        130        // The 'PICT' ID# where the clam masks are located.
  51. #define  rBackgroundID    134        // The 'PICT' ID# where the background picture is located.
  52. #define  rMainWindowID    128        // The 'WIND' resource ID# for the main window.
  53.  
  54. #define  rHelloSndID    3000
  55. #define  rFootstepSndID    3001
  56. #define  rImpactSndID    3002
  57. #define  rDizzySndID    3003
  58.  
  59. #define  kColorBitsNeeded    8
  60.     
  61. typedef struct
  62. {
  63.     Rect    face;
  64.     Rect    mask;
  65. } tSpriteType;
  66.  
  67.  
  68. Rect    bigPictureRect, masksRect,  allComboRect;
  69. Rect    janeFacesRect, janeIsAtRect, janeWasAtRect, janeComboRect;
  70. Rect    clemFacesRect, clemIsAtRect, clemWasAtRect, clemComboRect;
  71. CGrafPtr    workCPort, janeFacesCPort, clemFacesCPort, backgroundCPort;
  72. GrafPtr mainWindow, masksPort;
  73. Boolean    itWorked, contactFlag, impactReadyFlag, gameOverFlag, evenFrame;    // Note new flags.
  74. long    targetTick;
  75. short    janeSprite, clemSprite, thisFaceCounter;
  76.  
  77. tSpriteType    sprite[kMaxFaces];
  78.  
  79. extern    Boolean        gUserWantsSound;
  80.     
  81.     
  82. //==============================================================  Prototypes
  83.  
  84. void InitAll(void);
  85. void OpenMainWindow (void);
  86. void SetTheRects(void);
  87. void SetTheCPorts(void);
  88. void CopyBothAtOnce (void);
  89. void CopyOneAtATime (void);
  90. void ShowClams (void);
  91. void DoDelay (void);
  92. void JaneLoop (void);
  93. void ClemLoop (void);
  94. void JaneSpeaks (void);
  95. void DoImpact (void);
  96.  
  97. //==============================================================  Functions
  98.  
  99. //--------------------------------------------------------------  InitAll
  100.  
  101. void InitAll(void)
  102. {
  103.     InitToolbox();
  104.     if (WhatsOurDepth() != kColorBitsNeeded)    // Compare color depth with what…
  105.         RedAlert(kErrNot8BitColor);                // …we want. If not equal, exit.
  106.     gUserWantsSound = TRUE;
  107.     InitializeForSound();
  108.     SetTheRects();        // Since some of these Rects define fields in CGrafPorts,…
  109.     SetTheCPorts();        // …set the rects before setting the ports.
  110.     janeSprite = kStepRightFace;
  111.     clemSprite = kRunRightFace;
  112.     thisFaceCounter = 0;
  113.     contactFlag = FALSE;        // Not touching,
  114.     impactReadyFlag = FALSE;    // Not about to collide,
  115.     gameOverFlag = FALSE;        // Not done playing.
  116.     evenFrame = TRUE;
  117.     targetTick = TickCount() + kWaitTicks;
  118.     HideCursor();        // This demo doesn't use mouse input, so why look at it?
  119. }
  120.  
  121. //--------------------------------------------------------------  OpenMainWindow
  122.  
  123. void OpenMainWindow (void)
  124. {
  125.     mainWindow = GetNewCWindow(128, 0L, kPutInFront);    // Load window from resource.
  126.     ShowWindow((GrafPtr)mainWindow);                    // Now display it.
  127.     SetPort((GrafPtr)mainWindow);                        // Make its port current.
  128.     ClipRect(&bigPictureRect);                            // Set its clip region.
  129.     CopyRgn(mainWindow->clipRgn, mainWindow->visRgn);    // Set its visRgn.
  130.     ForeColor(blackColor);                                // Set its pen color to black.
  131.     BackColor(whiteColor);                                // Set background color white.
  132. }
  133.  
  134. //--------------------------------------------------------------  SetTheRects
  135.  
  136. void SetTheRects(void)    // The most tedious part of programming this type of game.
  137. {
  138.     SetRect(&janeFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  139.     SetRect(&clemFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  140.     SetRect(&masksRect, 0, 0, 448, 32);        // Size and shape of BitMap for the sprite masks.
  141.     SetRect(&bigPictureRect, 0, 0, 512, 322);    // The shape of the picture we'll put in the main window, workCPort and backgroundCPort.}
  142.     SetRect(&janeIsAtRect, 80, 240, 112, 272);    // The shape (32 x 32) of the images of Jane, and the position of the first image.}
  143.     janeWasAtRect = janeIsAtRect;                // Initializing janeIsAtRect...it has to start somewhere, and this is handy.}
  144.     janeComboRect = janeIsAtRect;
  145.     SetRect(&clemIsAtRect, 40, 244, 72, 276);    // The shape (32 x 32) of the images of Clem, and the position of the first image.}
  146.     clemWasAtRect = clemIsAtRect;                // Initializing clemIsAtRect...it has to start somewhere, and this is handy.}
  147.     clemComboRect = clemIsAtRect;
  148.         // And now, the tedium. In this sample, all we're doing is showing the clam running across the screen to the right.}
  149.         // However, if you use ResEdit and look at 'PICT' 129 in Sample.rsrc, you'll find 28 different views of the clam.}
  150.         // If we wanted the clam to run left too, and walk slowly, and face the user, and blink its eyes, we'd be calling…}
  151.         // …SetRect 56 times--one face and one mask per sprite. And if we had jumping clams and rear views of clams…}
  152.         // …and starfish and clamdiggers and other hazards of the clam environment, we might have hundreds of rects to set.}
  153.     SetRect(&sprite[kFrontFace].face, 320, 32, 352, 64);    // The shape and position of sprite[kFrontFace].face on facesCPort.portPixMap.
  154.     SetRect(&sprite[kFrontFace].mask, 320, 0, 352, 32);    // The shape and position of sprite[kFrontFace].mask on masksPort.portPixMap.
  155.     SetRect(&sprite[kBlinkFace].face, 320, 0, 352, 32);    // Note that some faces (e.g. eyes open or closed) use the same mask,…
  156.     SetRect(&sprite[kBlinkFace].mask, 320, 0, 352, 32);    // …since they have the same silhouette.}
  157.     SetRect(&sprite[kEehFace].face, 352, 0, 384, 32);    // I could write more comments here, but setting these rects…
  158.     SetRect(&sprite[kEehFace].mask, 352, 0, 384, 32);    // …is already tedious enough without a bunch of busy-work.
  159.     SetRect(&sprite[kOohFace].face, 352, 32, 384, 64);
  160.     SetRect(&sprite[kOohFace].mask, 352, 0, 384, 32);    
  161.     SetRect(&sprite[kStepRightFace].face, 192, 0, 224, 32);    
  162.     SetRect(&sprite[kStepRightFace].mask, 192, 0, 224, 32);    
  163.     SetRect(&sprite[kWalkRightFace].face, 224, 0, 256, 32);    
  164.     SetRect(&sprite[kWalkRightFace].mask, 224, 0, 256, 32);    
  165.     SetRect(&sprite[kRunRightFace].face, 160, 0, 192, 32);    // BTW, there are plenty more clam faces and masks in the 'PICT's,…
  166.     SetRect(&sprite[kRunRightFace].mask, 160, 0, 192, 32);    // …if you feel you need rect setting practice.  :-)
  167.     SetRect(&sprite[kDizzyRightFace1].face, 384, 0, 416, 32);        // Hey look! Here's more now!
  168.     SetRect(&sprite[kDizzyRightFace1].mask, 384, 0, 416, 32);    
  169.     SetRect(&sprite[kDizzyRightFace2].face, 384, 32, 416, 64);    
  170.     SetRect(&sprite[kDizzyRightFace2].mask, 384, 0, 416, 32);    
  171.     SetRect(&sprite[kDizzyLeftFace1].face, 416, 0, 448, 32);    
  172.     SetRect(&sprite[kDizzyLeftFace1].mask, 416, 0, 448, 32);    
  173.     SetRect(&sprite[kDizzyLeftFace2].face, 416, 32, 448, 64);    
  174.     SetRect(&sprite[kDizzyLeftFace2].mask, 416, 0, 448, 32);    
  175.     
  176. }
  177.  
  178. //--------------------------------------------------------------  SetTheCPorts
  179.  
  180. void SetTheCPorts(void)    // Create the CGrafPorts and load their .portPixMap fields.
  181. {
  182.             // Create BitMap for sprite masks. NOTE THIS IS A BITMAP!
  183.     CreateOffScreenBitMap (&masksRect, &masksPort);
  184.     LoadGraphic (rMasksID);        // …load 'PICT' resource for the clam masks.
  185.     
  186.             // Create PixMap for Jane faces.
  187.     CreateOffScreenPixMap (&janeFacesRect, &janeFacesCPort);
  188.     LoadGraphic (rJaneFacesID);        // …load 'PICT' resource for the clam faces.
  189.  
  190.             // Create PixMap for Clem faces.
  191.     CreateOffScreenPixMap (&clemFacesRect, &clemFacesCPort);
  192.     LoadGraphic (rClemFacesID);        // …load 'PICT' resource for the clam faces.
  193.  
  194.             // Create PixMap for the background.
  195.     CreateOffScreenPixMap (&bigPictureRect, &backgroundCPort);
  196.     LoadGraphic(rBackgroundID);    // …load 'PICT' resource for the background picture.
  197.     
  198.             // Create PixMap for offscreen graphics work.
  199.     CreateOffScreenPixMap (&bigPictureRect, &workCPort);
  200.  
  201.     OpenMainWindow();
  202.  
  203.         //{This fills the main window with the background picture, so the user can see it.
  204.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  205.         &((GrafPtr)mainWindow)->portBits, 
  206.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  207.  
  208. // This fills the workCPort.portPixMap with the background picture, so updates can be done quickly.
  209.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  210.         &((GrafPtr)workCPort)->portBits, 
  211.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  212. }
  213.  
  214. //--------------------------------------------------------------  CopyBothAtOnce
  215.  
  216. void CopyBothAtOnce (void)    //Display the clams on the screen when they're close together.
  217. {
  218.         //UnionRect(clemComboRect, larryComboRect, allComboRect);
  219.         //{Find the smallest rectangle which will cover Larry and Clem.}
  220.     UnionRect(&janeComboRect, &clemComboRect, &allComboRect);
  221. // Find the smallest rectangle which will cover the old position of Clem and the new.
  222.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  223.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  224.         &(((GrafPtr)mainWindow)->portBits), 
  225.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  226.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  227.         //{Restore the workPort by covering up our clams with the background they obscure.  This way, workPort is…}
  228.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  229.         &(((GrafPtr)workCPort)->portBits), 
  230.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  231. // Restore the workCPort by covering up our clam with the background it obscures.
  232. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  233. // without having to copy the entire PixMap.
  234. }
  235.  
  236. //--------------------------------------------------------------  CopyOneAtATime
  237.  
  238. void CopyOneAtATime (void)    // Display the clams on the screen when they're far apart.
  239. {
  240.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, larryComboRect, larryComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  241.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  242.         &(((GrafPtr)mainWindow)->portBits), 
  243.         &janeComboRect, &janeComboRect, srcCopy, mainWindow->visRgn);
  244. // Copy the contents of janeComboRect from workCPort->portPixMap to the main window. In one swell foop, old Jane…
  245. //…will be erased, and the new Jane overlayed onto the background picture. Wallah! Flicker-free animation!
  246.     //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, clemComboRect, clemComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  247.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  248.         &(((GrafPtr)mainWindow)->portBits), 
  249.         &clemComboRect, &clemComboRect, srcCopy, mainWindow->visRgn);
  250. // Copy the contents of clemComboRect from workCPort->portPixMap to the main window. In one swell foop, old Clem
  251. //…will be erased, and the new Clem overlayed onto the background picture. Wallah! Flicker-free animation!
  252.     //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, larryRect, larryRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  253.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  254.         &(((GrafPtr)workCPort)->portBits), 
  255.         &janeIsAtRect, &janeIsAtRect, srcCopy, mainWindow->visRgn);
  256. // Restore the workCPort by covering up Jane's image with the background it obscures.
  257. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  258. // without having to copy the entire PixMap.
  259.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  260.         &(((GrafPtr)workCPort)->portBits), 
  261.         &clemIsAtRect, &clemIsAtRect, srcCopy, mainWindow->visRgn);
  262. // Restore the workCPort by covering up Clem's image with the background it obscures.
  263. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  264. // without having to copy the entire PixMap.
  265.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, clemRect, clemRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  266. }
  267.  
  268. //--------------------------------------------------------------  ShowClam 
  269.  
  270. void ShowClams (void)    // Do the animation and make it appear on the screen.
  271. {
  272.     Rect dummyRect;
  273.     
  274.     CopyMask(&((GrafPtr)janeFacesCPort)->portBits, 
  275.         &((GrafPtr)masksPort)->portBits, 
  276.         &((GrafPtr)workCPort)->portBits, 
  277.         &sprite[janeSprite].face, 
  278.         &sprite[janeSprite].mask, 
  279.         &janeIsAtRect);
  280. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  281. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  282. // previous image of the clam visible on the screen while we worked.
  283.  
  284.     UnionRect(&janeWasAtRect, &janeIsAtRect, &janeComboRect);
  285. // Find the smallest rectangle which will cover the old position of Jane and the new.
  286.  
  287. CopyMask(&((GrafPtr)clemFacesCPort)->portBits, 
  288.         &((GrafPtr)masksPort)->portBits, 
  289.         &((GrafPtr)workCPort)->portBits, 
  290.         &sprite[clemSprite].face, 
  291.         &sprite[clemSprite].mask, 
  292.         &clemIsAtRect);
  293. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  294. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  295. // previous image of the clam visible on the screen while we worked.
  296.  
  297.     UnionRect(&clemWasAtRect, &clemIsAtRect, &clemComboRect);
  298. // Find the smallest rectangle which will cover the old position of Clem and the new.
  299.  
  300.  
  301.     if (SectRect(&clemIsAtRect, &janeIsAtRect, &dummyRect))
  302.     {
  303.         contactFlag = TRUE;
  304.         CopyBothAtOnce();
  305.     }
  306.     else
  307.     {
  308.         contactFlag = FALSE;
  309.         CopyOneAtATime();
  310.     }
  311. }
  312. //    CopyBits(&((GrafPtr)workCPort)->portBits, 
  313.         //&(((GrafPtr)mainWindow)->portBits), 
  314.         //&clamComboRect, &clamComboRect, srcCopy, mainWindow->visRgn);
  315. // Copy the contents of comboRect from workCPort->portPixMap to the main window. In one swell foop, the old clam…}
  316. //…will be erased, and the new clam overlayed onto the background picture. Wallah! Flicker-free animation!}
  317.  
  318.     //CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  319.         //&(((GrafPtr)workCPort)->portBits), 
  320.         //&clamIsAtRect, &clamIsAtRect, srcCopy, mainWindow->visRgn);
  321. // Restore the workCPort by covering up our clam with the background it obscures.
  322. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  323. // without having to copy the entire PixMap.
  324. //}
  325.  
  326. //--------------------------------------------------------------  DoDelay
  327.  
  328. // This is the companion function to the above function (LogNextTick()).
  329. // We do nothing but loop until TickCount() catches up with (or passes) our…
  330. // global variable tickNext.
  331.  
  332. void DoDelay (void)
  333. {
  334.     do
  335.     {
  336.     }
  337.     while (TickCount() < targetTick);            // Loop until TickCount() catches up.
  338.     targetTick = TickCount() + kWaitTicks;
  339. }
  340.  
  341. //--------------------------------------------------------------  JaneLoop
  342.  
  343. void JaneLoop (void)
  344. {
  345.     switch    (janeSprite)                    //If the current view of jane is…
  346.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  347.         {    janeSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  348.             break;    }
  349.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  350.         {    janeSprite = kRunRightFace;        // …kRunRightFace.
  351.             break;    }
  352.     // And if it was neither kStepRightFace nor kWalkRightFace, then janeSprite was either…
  353.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  354.             janeSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  355.     }
  356.     
  357.     janeWasAtRect = janeIsAtRect;            // Store the clam's current position as its last position,…
  358.                                             // …we'll be erasing it next time through the loop.
  359.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position--it'll be…
  360.                                             // …kStepLength pixels to the right of its last position.
  361.     if (janeIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  362.     {                                        // …set the right border of clamIsAtRect…
  363.         janeIsAtRect.right = 0;                // …to the left edge of the screen…
  364.         janeIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  365.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  366. }
  367.  
  368. //--------------------------------------------------------------  ClemLoop
  369.  
  370. void ClemLoop (void)
  371. {
  372.     switch    (clemSprite)                    //If the current view of Clem is…
  373.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  374.         {    clemSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  375.             break;    }
  376.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  377.         {    clemSprite = kRunRightFace;        // …kRunRightFace.
  378.             break;    }
  379.     // And if it was neither kStepRightFace nor kWalkRightFace, then clemSprite was either…
  380.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  381.             clemSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  382.     }
  383.     clemWasAtRect = clemIsAtRect;            // Store the clam's current position as its last position,…
  384.                                             // …we'll be erasing it next time through the loop.
  385.     OffsetRect(&clemIsAtRect, kClemStepLength, 0);    // Set the clam's next position--it'll be…
  386.                                             // …kStepLength pixels to the right of its last position.
  387.     if (clemIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  388.     {                                        // …set the right border of clamIsAtRect…
  389.         clemIsAtRect.right = 0;                // …to the left edge of the screen…
  390.         clemIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  391.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  392.     if (clemSprite == kStepRightFace)                // Only sound a footstep in pose where foot strikes ground.
  393.           PlayASound(rFootstepSndID, kLowSoundPriority);        //Ahh, the pitter patter of tiny feet.
  394. }
  395.  
  396. //--------------------------------------------------------------  JaneSpeaks
  397.  
  398. // This routine has the clam stop moving, face the screen, and move its face.
  399.  
  400. void JaneSpeaks (void)
  401. {    Rect dummyRect;
  402.     if ((impactReadyFlag) && (SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))
  403.         DoImpact();
  404.     thisFaceCounter = thisFaceCounter + 1;
  405.          switch (thisFaceCounter)
  406.          {    case    (1): 
  407.             {    janeSprite = kFrontFace;
  408.                 break;    }
  409.             case    (2): 
  410.             {    janeSprite = kEehFace;
  411.                 break;    }
  412.             case    (3):     // This keeps an impact from occurring when both clams are in the same…
  413.             {    if (!(SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))    // …place when the…
  414.                     impactReadyFlag = TRUE;                                    // …button is pushed.
  415.                 break;    }
  416.             case    (4): 
  417.             {    janeSprite = kFrontFace;
  418.                 break;    }
  419.             case    (5): 
  420.             {    janeSprite = kOohFace;
  421.                 break;    }
  422.             case    (9): 
  423.             {    janeSprite = kEehFace;
  424.                 break;    }
  425.             case    (11): 
  426.             {    janeSprite = kOohFace;
  427.                 break;    }
  428.             case    (13): 
  429.             {    janeSprite = kFrontFace;
  430.                 break;    }
  431.             case    (16): 
  432.             {    janeSprite = kBlinkFace;
  433.                 break;    }
  434.             case    (18): 
  435.             {    janeSprite = kFrontFace;
  436.                 break;    }
  437.             case    (35): 
  438.             {    janeSprite = kBlinkFace;
  439.                 break;    }
  440.             case    (38): 
  441.             {    janeSprite = kFrontFace;
  442.                 break;    }
  443.             case    (39): 
  444.             {    impactReadyFlag = TRUE;
  445.                 break;    }
  446.         }
  447. }
  448.  
  449. //--------------------------------------------------------------  DoImpact
  450.  
  451. void DoImpact (void)
  452. {
  453.     janeWasAtRect = janeIsAtRect;    // Store the clam's current position as its last position.
  454.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position.
  455.     clemWasAtRect = clemIsAtRect;        // Store the clam's current position as its last position.
  456.     OffsetRect(&clemIsAtRect, kClemStepLength * kBounce, 0);    // Set the clam's next position.
  457.     PlayASound(rImpactSndID, kHighestSoundPriority);        // High enough to interrupt anything.
  458.     thisFaceCounter = 0;
  459.     while (thisFaceCounter <= kLastFace)
  460.     {
  461.         ShowClams();
  462.         DoDelay();                // Do nothing for a while.
  463.         PlayLoopSound(rDizzySndID, kHighSoundPriority);    // Not high enough to interrupt the impact.
  464.         thisFaceCounter = thisFaceCounter + 1;
  465.         evenFrame = !evenFrame;
  466.         if (evenFrame)
  467.         {
  468.             janeSprite = kDizzyLeftFace1;
  469.             clemSprite = kDizzyRightFace1;
  470.         }
  471.         else
  472.         {
  473.             janeSprite = kDizzyLeftFace2;
  474.             clemSprite = kDizzyRightFace2;
  475.         }
  476.     }
  477.     gameOverFlag = TRUE;        // And we're outta here. Game over.
  478. }
  479.  
  480. //--------------------------------------------------------------  main
  481. //----------------------------------------------------------------------
  482.  
  483. void main(void)
  484. {
  485.     InitAll();
  486.     while (!Button())    // Before the user presses the mouse button, nothing happens.
  487.         {
  488.         }
  489.     while (Button())    // When the user presses the mouse button, nothing happens.
  490.         {
  491.         }
  492.     while (!Button())    // When the mouse button is released, continue to…
  493.         {
  494.         ShowClams();    // Put the clams on the screen.
  495.         DoDelay();        // Keep everything from happening too fast.
  496.         JaneLoop();        // Put Jane in her next position.
  497.         ClemLoop();        // Put Clem in his next position.
  498.         }
  499.     PlayASound(rHelloSndID, kMediumSoundPriority);    // Sound, "Hello" when button is pushed.
  500.     while (!(gameOverFlag))    // …until the clams crash into each other and fall down dizzy.
  501.         {    
  502.         ShowClams();    // Put the clams on the screen.
  503.         DoDelay();        // Keep everything from happening too fast.
  504.         JaneSpeaks();    // Make mouth motions, shift into impact mode when ready.
  505.         ClemLoop();        // Put Clem in his next position.
  506.         }
  507.     CloseDownSound();
  508.     InitCursor();    // Rarely needed, since most programs call InitCursor on startup. Still, it's…
  509.                     // a good habit to leave everything in normal condition when your programs quit.
  510. }                    // And we're done.
  511.  
  512. //------------------------------------------------------------------------------------------\\
  513. //                                    End HelloWorld4.c                                        \\
  514. //------------------------------------------------------------------------------------------\\
  515.